package com.example.monitorsms;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.alibaba.fastjson.JSON;
import com.example.monitorsms.http.CommonHttpConsts;
import com.example.monitorsms.http.HttpCallbackData;
import com.example.monitorsms.http.HttpClient;
import com.example.monitorsms.utils.DialogUitl;
import com.example.monitorsms.utils.SPUtils;
import com.example.monitorsms.utils.StringUtils;

import java.util.Map;

public class LoginActivity extends BaseActivity {
    private EditText et_account, et_password;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void main(Bundle savedInstanceState) {
        super.main(savedInstanceState);
        et_account = findViewById(R.id.et_account);
        et_password = findViewById(R.id.et_account);

        findViewById(R.id.tvRegister).setOnClickListener(v -> {
            startActivity(new Intent(mContext, RegisterActivity.class));
        });

        findViewById(R.id.tvForget).setOnClickListener(v -> {
            startActivity(new Intent(mContext, MainActivity.class));
            LoginActivity.this.finish();
        });

        findViewById(R.id.btn_login).setOnClickListener(v -> {
            String account = et_account.getText().toString().trim();
            String password = et_password.getText().toString().trim();
            if (StringUtils.isEmpty(account) || StringUtils.isEmpty(password)) {
                Toast.makeText(mContext, "账号、密码不能为空！", Toast.LENGTH_SHORT).show();
            }
            if (!StringUtils.isMobileNO(account)) {
                Toast.makeText(mContext, "手机号输入格式不正确！", Toast.LENGTH_SHORT).show();
                return;
            }

            login(account, password);
        });
    }

    private void login(String account, String password) {
        HttpClient.getInstance().postData(CommonHttpConsts.login, "login")
                .params("account", account)
                .params("password", password)
                .execute(new HttpCallbackData() {
                    @Override
                    public void onSuccess(int code, String msg, String info) {
                        if (code == 1) {
                            Map dataMap = JSON.parseObject(info, Map.class);
                            //score 1 激活，0 未激活
                            SPUtils.put(mContext, "token", dataMap.get("token"));
                            Double score = Double.valueOf(StringUtils.isEmpty(dataMap.get("score") + "") ? "0" : dataMap.get("score") + "");
                            if (score == 1) {
                                startActivity(new Intent(mContext, MainActivity.class));
                                LoginActivity.this.finish();
                            } else {
                                numDialog();
                            }
                        } else {
                            Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public boolean showLoadingDialog() {
                        return true;
                    }

                    @Override
                    public Dialog createLoadingDialog() {
                        return DialogUitl.loginAuthDialog(mContext);
                    }
                });
    }

    public void numDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.dialog);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.dialog_num, null);
        ImageView tvClose = v.findViewById(R.id.tvClose);
        TextView tvDelete = v.findViewById(R.id.tvDelete);
        TextView tvSave = v.findViewById(R.id.tvSave);
        EditText etName = v.findViewById(R.id.etName);
        TextView tvNumType = v.findViewById(R.id.tvNumType);
        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(true);//点击屏幕不消失

        if (!dialog.isShowing()) {//此时提示框未显示
            dialog.show();
        }
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
        //自定义的东西
        dialog.getWindow().setContentView(v);
        tvNumType.setVisibility(View.VISIBLE);
        tvClose.setOnClickListener(v1 -> dialog.dismiss());

        tvDelete.setOnClickListener(view -> {
            dialog.dismiss();
        });

        tvSave.setOnClickListener(view -> {
            String text = etName.getText().toString().trim();
            if (StringUtils.isEmpty(text)) {
                Toast.makeText(mContext, "请输入激活码", Toast.LENGTH_SHORT).show();
                return;
            }
//            SPUtils.put(mContext, "userType", true);
//            tablesNum = Double.valueOf(text).intValue();
//            if (newHasList.size() > 0) {
//                orderBuyV2num();
//            }
            dialog.dismiss();
        });
    }

}
